package com.modouyu.novel.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.modouyu.common.utils.PageUtils;
import com.modouyu.novel.entity.NovelArticleEntity;
import com.modouyu.novel.entity.NovelChapterEntity;
import com.modouyu.novel.entity.SysDictEntity;
import com.modouyu.novel.service.NovelArticleService;
import com.modouyu.novel.service.NovelChapterService;
import com.modouyu.novel.service.SysDictService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2018/5/7.
 */
@Controller
public class IndexController {

    @Autowired
    private NovelArticleService novelArticleService;
    @Autowired
    private NovelChapterService novelChapterService;

    @Autowired
    private SysDictService sysDictService;

    @RequestMapping(value = {"", "/", "index.html"}, method = RequestMethod.GET)
    public String index() {
        return "index";
    }


    @RequestMapping(value = {"book/new.html"}, method = RequestMethod.GET)
    public String newbook(){
        return "new";
    }

    @RequestMapping(value = {"book/top.html"}, method = RequestMethod.GET)
    public String top(){
        return "top";
    }



    @RequestMapping(value = "book",method = RequestMethod.GET)
    public String book(HttpServletRequest request){
        PageUtils page = novelArticleService.queryPage("bookstore", 1);
        request.setAttribute("channel", this.getSysDictEntity("0","quanbu"));
        request.setAttribute("type", "default");
        request.setAttribute("letter", "0");
        request.setAttribute("size", 0);
        request.setAttribute("fullflag", 0);
        request.setAttribute("sort", 0);
        request.setAttribute("page", page);
        return "default-0-0-0-0-0-0-1";
    }

    @RequestMapping(value = {"book/channel/list/{typeremark}/{typecode}/{type}-{letter}-{size}-0-0-{fullflag}-{sort}-{pageno}.html"}, method = RequestMethod.GET)
    public String channel(@PathVariable String typeremark, @PathVariable String typecode,@PathVariable String type,@PathVariable Integer pageno,@PathVariable String letter, @PathVariable Integer size, @PathVariable Integer fullflag, @PathVariable Integer sort, HttpServletRequest request) {

        PageUtils page = novelArticleService.queryPage(typeremark, pageno);
        request.setAttribute("channel", this.getSysDictEntity(typecode,typeremark));
        request.setAttribute("type", type);
        request.setAttribute("letter", letter);
        request.setAttribute("size", size);
        request.setAttribute("fullflag", fullflag);
        request.setAttribute("sort", sort);
        request.setAttribute("page", page);
        return "default-0-0-0-0-0-0-1";
    }




    private SysDictEntity getSysDictEntity(String typecode,String typeremark){

        if("quanbu".equalsIgnoreCase(typeremark)){
            SysDictEntity sysDictEntity=new SysDictEntity();
            sysDictEntity.setCode("0");
            sysDictEntity.setId(0L);
            sysDictEntity.setName("全部小说");
            sysDictEntity.setRemark("quanbu");
            return sysDictEntity;
        }

        Wrapper<SysDictEntity> ew=new EntityWrapper<SysDictEntity>();
        ew.eq("type","novel_type").eq("code",typecode);
        return sysDictService.selectOne(ew);

    }

    @RequestMapping(value = {"book/{typeid}/{articleid}/", "book/{typeid}/{articleid}/detail.html", "book/{typeid}/{articleid}.html"}, method = RequestMethod.GET)
    public String articleDetail(@PathVariable Integer typeid, @PathVariable Integer articleid, HttpServletRequest request) {

        NovelArticleEntity novelArticleEntity = novelArticleService.selectById(articleid);
        List<NovelChapterEntity> chapterList = novelChapterService.selectList(new EntityWrapper<NovelChapterEntity>().eq("articleid", articleid));
        request.setAttribute("article", novelArticleEntity);
        request.setAttribute("chapterList", chapterList);
        return "detail";
    }

    @RequestMapping(value = {"book/{typeid}/{articleid}/{chapterid}", "book/{typeid}/{articleid}/{chapterid}.html", "book/{typeid}/{articleid}/{chapterid}/detail.html"}, method = RequestMethod.GET)
    public String chapterDetail(@PathVariable Integer typeid, @PathVariable Integer articleid, @PathVariable Integer chapterid, HttpServletRequest request) {

        NovelChapterEntity chapterEntity = novelChapterService.selectOne(new EntityWrapper<NovelChapterEntity>().eq("chapterid", chapterid));
        NovelChapterEntity preChapter = novelChapterService.selectPreChapter(chapterEntity);
        NovelChapterEntity nextChapter = novelChapterService.selectNextChapter(chapterEntity);
        request.setAttribute("chapter", chapterEntity);
        request.setAttribute("preChapter", preChapter);
        request.setAttribute("nextChapter", nextChapter);
        return "chapter";
    }

    @RequestMapping(value = "/book/article/articlevisit.html")
    @ResponseBody
    public String articlevisit(Integer id) {
        novelArticleService.updateArticleVisit(id);
        return "SUCCESS";
    }

    @RequestMapping(value = "/book/article//uservote.html")
    @ResponseBody
    public String uservote(Integer id) {
        novelArticleService.updateArticleVote(id);
        return "SUCCESS";
    }

}
